package com.sunye.shardingjdbc;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sunye.dao.CourseDao;
import com.sunye.dao.CourseUserInfoDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("courseController")
public class CourseController {


    @Autowired
    private CourseDao courseDao;

    @Autowired
    private CourseUserInfoDao courseUserInfoDao;



    @RequestMapping("/addCourse")
    public void addCourse(){
        for(int i=1;i<=10;i++) {
            Course course = new Course();
            course.setCname("java"+i);
            course.setUserId(i%2==0? 100L:200L);
            course.setCstatus(i);
            courseDao.insert(course);
            System.out.println("course = " + course);
        }
        QueryWrapper<CourseUserInfo> queryWrapper = new QueryWrapper();
        Integer count = courseUserInfoDao.selectCount(queryWrapper);
        if(count==0){
            CourseUserInfo userInfo = new CourseUserInfo();
            userInfo.setUserId(100L);
            userInfo.setTeacherName("张三");
            courseUserInfoDao.insert(userInfo);

            CourseUserInfo userInfo2 = new CourseUserInfo();
            userInfo2.setUserId(200L);
            userInfo2.setTeacherName("李四");
            courseUserInfoDao.insert(userInfo2);
        }
    }


    @RequestMapping("/findCourse")
    public List<Course> findCourse(){
        QueryWrapper<Course> queryWrapper = new QueryWrapper();
        List<Course> courses = courseDao.selectList(queryWrapper);
        return courses;
    }


    @RequestMapping("/findCourseById")
    public List<Course> findCourseById(Long id){
        if(id == null){
            throw new RuntimeException("id不能为空");
        }
        QueryWrapper<Course> queryWrapper = new QueryWrapper();
        queryWrapper.eq("cid",id);
        List<Course> courses = courseDao.selectList(queryWrapper);
        return  courses;
    }


    @RequestMapping("/findCourseGt")
    public List<Course> findCourseGt(){
        QueryWrapper<Course> queryWrapper = new QueryWrapper();
        queryWrapper.gt("cstatus","5");
        List<Course> courses = courseDao.selectList(queryWrapper);
        return  courses;
    }


    //http://localhost:8088/courseController/findCourseIn?id=841321556419280897&id2=841321556956151808
    @RequestMapping("/findCourseIn")
    public List<Course> findCourseIn(Long id,Long id2){
        if(id == null || id2 == null){
            throw new RuntimeException("id不能为空");
        }
        QueryWrapper<Course> queryWrapper = new QueryWrapper();
        queryWrapper.in("cid",id,id2);
        List<Course> courses = courseDao.selectList(queryWrapper);
        return  courses;
    }


    //关联表 查询 参考
    //https://blog.csdn.net/Curtisjia/article/details/127018341
    //关联表查询
    @RequestMapping("/findCourseJoin")
    public List<ResponseCourse> findCourseJoin(){
        List<ResponseCourse> courses = courseDao.findCourseJoin();
        return  courses;
    }
}
